package com.muyu.system.service.impl;

import com.muyu.common.core.utils.StringUtils;
import com.muyu.system.domain.DanmoShoppingCategory;
import com.muyu.system.domain.DanmoShoppingSubcategory;
import com.muyu.system.mapper.DanmoShoppingCategoryMapper;
import com.muyu.system.service.IDanmoShoppingCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品分类Service业务层处理
 *
 * @author yangle
 * @date 2024-09-26
 */
@Service
public class DanmoShoppingCategoryServiceImpl implements IDanmoShoppingCategoryService
{
    @Autowired
    private DanmoShoppingCategoryMapper danmoShoppingCategoryMapper;

    /**
     * 查询商品分类
     *
     * @param id 商品分类主键
     * @return 商品分类
     */
    @Override
    public DanmoShoppingCategory selectDanmoShoppingCategoryById(Long id)
    {
        return danmoShoppingCategoryMapper.selectDanmoShoppingCategoryById(id);
    }

    /**
     * 查询商品分类列表
     *
     * @param danmoShoppingCategory 商品分类
     * @return 商品分类
     */
    @Override
    public List<DanmoShoppingCategory> selectDanmoShoppingCategoryList(DanmoShoppingCategory danmoShoppingCategory)
    {
        return danmoShoppingCategoryMapper.selectDanmoShoppingCategoryList(danmoShoppingCategory);
    }

    /**
     * 新增商品分类
     *
     * @param danmoShoppingCategory 商品分类
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDanmoShoppingCategory(DanmoShoppingCategory danmoShoppingCategory)
    {
        int rows = danmoShoppingCategoryMapper.insertDanmoShoppingCategory(danmoShoppingCategory);
        insertDanmoShoppingSubcategory(danmoShoppingCategory);
        return rows;
    }

    /**
     * 修改商品分类
     *
     * @param danmoShoppingCategory 商品分类
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDanmoShoppingCategory(DanmoShoppingCategory danmoShoppingCategory)
    {
        danmoShoppingCategoryMapper.deleteDanmoShoppingSubcategoryByCategoryId(danmoShoppingCategory.getId());
        insertDanmoShoppingSubcategory(danmoShoppingCategory);
        return danmoShoppingCategoryMapper.updateDanmoShoppingCategory(danmoShoppingCategory);
    }

    /**
     * 批量删除商品分类
     *
     * @param ids 需要删除的商品分类主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDanmoShoppingCategoryByIds(Long[] ids)
    {
        danmoShoppingCategoryMapper.deleteDanmoShoppingSubcategoryByCategoryIds(ids);
        return danmoShoppingCategoryMapper.deleteDanmoShoppingCategoryByIds(ids);
    }

    /**
     * 删除商品分类信息
     *
     * @param id 商品分类主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDanmoShoppingCategoryById(Long id)
    {
        danmoShoppingCategoryMapper.deleteDanmoShoppingSubcategoryByCategoryId(id);
        return danmoShoppingCategoryMapper.deleteDanmoShoppingCategoryById(id);
    }

    /**
     * 新增商品子分类信息
     *
     * @param danmoShoppingCategory 商品分类对象
     */
    public void insertDanmoShoppingSubcategory(DanmoShoppingCategory danmoShoppingCategory)
    {
        List<DanmoShoppingSubcategory> danmoShoppingSubcategoryList = danmoShoppingCategory.getDanmoShoppingSubcategoryList();
        Long id = danmoShoppingCategory.getId();
        if (StringUtils.isNotNull(danmoShoppingSubcategoryList))
        {
            List<DanmoShoppingSubcategory> list = new ArrayList<DanmoShoppingSubcategory>();
            for (DanmoShoppingSubcategory danmoShoppingSubcategory : danmoShoppingSubcategoryList)
            {
                danmoShoppingSubcategory.setCategoryId(id);
                list.add(danmoShoppingSubcategory);
            }
            if (list.size() > 0)
            {
                danmoShoppingCategoryMapper.batchDanmoShoppingSubcategory(list);
            }
        }
    }
}
